//
//  RootPilotViewController.m
//  EVETracker
//
//  Created by Scott Guyer on 7/19/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "RootPilotViewController.h"
#import "PilotTabViewController.h"
#import "PilotListViewController.h"
#import "PilotSettingsViewController.h"
#import "PilotSkillsVC.h"
#import "Character.h"
#import "defs.h"


@implementation RootPilotViewController

@synthesize pilots;


- (id) init
{
	[super init];
	
	pilotTabVC = [[PilotTabViewController alloc] 
				  initWithNibName:@"PilotTabView" 
				  bundle:nil];
	pilotTabVC.rootPilotVC = self;
	
	pilotSettingsVC = [[PilotSettingsViewController alloc] 
					   initWithNibName:@"PilotSettingsView" bundle:nil];
	pilotSettingsVC.rootPilotVC = self;
	
	self.tabBarItem.title = [NSString stringWithString:@"Pilots"];
	self.tabBarItem.image = [UIImage imageNamed:@"pilot.png"];
	
	pilots = [Character loadPilots];
	if ( (pilots == nil) || ([pilots count] <= 0) )
	{
		pilots = [[NSMutableArray alloc] initWithCapacity:10];
		[self showSettings];
	}
	else
	{
		[self showPilots];
	}
	
	[pilotTabVC setPilotList:pilots];
	[pilotSettingsVC setPilotList:pilots];
	
	CGRect f = self.view.frame;
	// I'm in a tab bar
	f.size.height -= 50;
	[self.view setFrame:f];
	
	[self.view setAutoresizesSubviews:YES];
	
	return self;
}


- (void) showPilots 
{
	if ( viewMode == PilotViewModeSkills )
	{
		[pilotSkillsVC viewWillDisappear:NO];
		[pilotSkillsVC.view removeFromSuperview];
		[pilotSkillsVC viewDidDisappear:NO];
		
		[pilotTabVC viewWillAppear:NO];
		[self.view addSubview:pilotTabVC.view];
		[pilotTabVC viewDidAppear:NO];			
	}
	else
	{
	
	if ( [pilotSettingsVC.view superview] != nil )
	{				
		[pilotTabVC viewWillAppear:YES];
		
		[pilotTabVC resyncModel];
		[pilotTabVC loadScrollViewWithPage:0];
		[pilotTabVC loadScrollViewWithPage:1];
				
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.6];
		
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
							   forView:self.view cache:YES];
		
		[pilotSettingsVC.view removeFromSuperview];
		[self.view addSubview:pilotTabVC.view];
		
		[UIView commitAnimations];		
		[pilotTabVC viewDidAppear:YES];
	}
	else
	{
		[pilotTabVC viewWillAppear:NO];
		[self.view addSubview:pilotTabVC.view];
		[pilotTabVC viewDidAppear:NO];
	}
		
	}
	
	viewMode = PilotViewModePager;
}


- (void) showSettings 
{
	if ( [pilotTabVC.view superview] != nil )
	{		
		[pilotSettingsVC viewWillAppear:YES];
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.6];
	
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
							   forView:self.view cache:YES];

		[pilotTabVC.view removeFromSuperview];
		[self.view addSubview:pilotSettingsVC.view];	
			
		[UIView commitAnimations];
		
		[pilotSettingsVC viewDidAppear:YES];
	}
	else
	{
		[pilotSettingsVC viewWillAppear:NO];
		[self.view addSubview:pilotSettingsVC.view];	
		[pilotSettingsVC viewDidAppear:NO];		
	}
	
	viewMode = PilotViewModeSettings;
}

- (void) showPilotSkills:(Character*)pilot 
{
	if ( pilotSkillsVC == nil )
	{
		pilotSkillsVC = [[PilotSkillsVC alloc] initWithNibName:@"PilotSkillsView" bundle:nil];
		pilotSkillsVC.rootPilotVC = self;
	}
	
	[pilotTabVC viewWillDisappear:NO];
	[pilotTabVC.view removeFromSuperview];
	[pilotTabVC viewDidDisappear:NO];

	[pilotSkillsVC viewWillAppear:NO];
	[self.view addSubview:pilotSkillsVC.view];
	[pilotSkillsVC updateViewWithPilot:pilot];
	[pilotSkillsVC viewDidAppear:NO];
	
	viewMode = PilotViewModeSkills;
}


- (void) replacePilot:(Character*)pilot atIndex:(NSUInteger)idx
{
	[pilots replaceObjectAtIndex:idx withObject:pilot];
}


- (int) updateCompletedPilotsCount
{
	int count = 0;
	
	NSCalendar* lcal = [NSCalendar currentCalendar];
	NSTimeZone* zulu = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
	NSCalendar* zcal = [NSCalendar currentCalendar];
	[zcal setTimeZone:zulu];
	
	NSDate* now = [NSDate date];	
	NSDateComponents* comp = [lcal components:0x03ff fromDate:now];
	NSDate* znow = [zcal dateFromComponents:comp];
		
	NSDate* endDate;
	NSTimeInterval diff;
	NSString* training;
	for ( Character* p in pilots )
	{
		
		training = (NSString*)[p.skillTrainingDict valueForKey:@"training"];
		
		// will be 0 if this pilot is not the last known trainer for its account
		// the EVE API seems to be buggy here, the flag is actually still set to 1 even though
		// another pilot on the account is training.
		
		endDate = (NSDate*)[p.skillTrainingDict valueForKey:@"endDate"];

		if ( (endDate != nil) && ( (training != nil) && ([training isEqualToString:@"1"]) ) )
		{
			diff = [znow timeIntervalSinceDate:endDate];
			if ( diff >= 0 )
				count ++;
		}
	}
	
	if ( self.tabBarItem != nil )
	{
		if ( count > 0 )
			[self.tabBarItem setBadgeValue:[NSString stringWithFormat:@"%d", count]];
		else
			[self.tabBarItem setBadgeValue:nil];
	}
	
	return count;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void) didReceiveMemoryWarning
{

}



- (void) jenny
{
	NSLog(@"%s", __FUNCTION__);
	if ( (viewMode != PilotViewModeSkills) && (pilotSkillsVC != nil) )
	{
		[pilotSkillsVC jenny];
		WIPE( pilotSkillsVC)
	}
}


- (void)dealloc 
{
	NSLog(@"%s 0x%x", __FUNCTION__, self);
	
	WIPE( pilotTabVC)
	WIPE( pilotSettingsVC)
	WIPE( pilots)
	WIPE( pilotSkillsVC)
	
	[super dealloc];
}


@end
